package pearson.automation.forms;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import pearson.automation.assets.AssetList;
import pearson.automation.assets.AssetWithOutRollOver;
import pearson.automation.assets.AssetWithRollOver;

import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;

public class SearchResultsForm extends BaseFormWithSidebarsAndSearch {
    private static Button allResults = new Button(By.linkText("All"), "All");

    private static Label alertAlreadyAddedInTheLibrary = new Label(
        By.xpath("//div[contains(.,'This item is already in your library')]"), "All");
    private static Button closeAlreadyAddedInTheLibrary = new Button(By.linkText("Close"), "Close");
    private static Button loadMore = new Button(By.linkText("Load more"), "Load more");
    private Label showing = new Label(By.linkText("//span[contains[text(),'loading']]"), "Showing");
    public AssetList searchResults = new AssetList("//div[@id='search-results']", "//li[contains(@class,'srl-item')]");
    public final ElementList category = new ElementList(By.xpath("//nav[contains(@class,'content-type-nav')]"),
        "Categories", "//nav[contains(@class,'content-type-nav')]//li[contains(.,'%1$s')]");

    public enum AssetCategory {
        ALL("All"),
        COURSES("Courses"),
        BOOKS("Books"),
        DOCUMENTS("Documents"),
        IMAGES("Images"),
        VIDEOS("Videos"),
        INTERACTIVE("Interactive");

        String name;
    
        AssetCategory(String val) {
            name = val;
    }

    @Override
    public String toString() {
        return name;
    }

    public String toAPIString() {
        String val = name.toUpperCase();
        if (val.endsWith("S")) {
        val = val.substring(0, val.length() - 1);
        }
        return val;
    }
    }

    public SearchResultsForm() {
    super(allResults.getLocator(), "Search results");
    }

    public SearchResultsForm openAssetTypeCategory(AssetCategory filter) {
    Button clickFilter = new Button(By.linkText(filter.toString()), filter.toString());
    clickFilter.clickAndWait();
    return new SearchResultsForm();
    }

    public int showAdditionalResults() {
    // Scroll to the bottom of the page
    ((JavascriptExecutor) browser.getDriver()).executeScript(
            "li_array = document.body.getElementById('search-results')" +
            ".getElementsByTagName('li');" +
            "li_array[li_array.length-1].scrollIntoView()");
    //Workaround for http://futurestate.jira.com/browse/PCLNG-3802
    if (loadMore.isPresent()) {
        loadMore.waitForElementDissapear();
    }
    showing.waitForElementDissapear();
    // Get new count of viewed assets
    return getResultsCount();
    }

    public int getResultsCount() {
    return browser.getDriver().findElementsByClassName("srl-item").size();
    }

    public int getNumberOfAssetsByThumbnailTitle(String url) {
    return browser.getDriver().findElementsByXPath(String.format("//img[contains(@title,\'%s\')]", url)).size();
    }

    public int getNumberOfAssetsWithTitle(String title) {
    return browser.getDriver().findElementsByXPath(String.format("//a[contains(.,\'%s\')]", title)).size();
    }

    public AssetWithRollOver getAssetWithRollOverByTitle(String title) {
    return new AssetWithRollOver(title);
    }

    public AssetWithRollOver getAssetWithRollOverByTitle(String title, int i) {
    return new AssetWithRollOver(title, i);
    }

    public AssetWithOutRollOver getAssetWithOutRollOverByTitle(String title) {
    return new AssetWithOutRollOver(title);
    }

    public AssetWithOutRollOver getAssetWithOutRollOverByTitle(String title, int index) {
    return new AssetWithOutRollOver(title, index);
    }

    public boolean isAlertAlreadyInLibraryAppeared() {
    return alertAlreadyAddedInTheLibrary.isPresent();
    }

    public void closeAlertAlreadyInLibrary() {
    closeAlreadyAddedInTheLibrary.clickAndWait();
    }

    public void selectCategory(String linkText) {
    category.selectListElement(linkText);
    }
}
